package com.bzzoneit.newsys.web.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bzzoneit.newsys.entity.admin.Menu;
import com.bzzoneit.newsys.entity.admin.Role;
import com.bzzoneit.newsys.service.admin.MenuService;
import com.bzzoneit.newsys.service.admin.RoleService;
import com.bzzoneit.newsys.service.admin.UserService;
import com.bzzoneit.newsys.utils.dwz.AjaxDone;
import com.bzzoneit.newsys.utils.dwz.JsonResult;
import com.bzzoneit.newsys.utils.web.Exceptions;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("/rolemenu")
public class UserRoleMenuController extends BaseController {
	
	@Autowired
	private MenuService menuService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private UserService userService;
	
	private List<Role> list;

	

	public List<Role> getList() {
		return list;
	}

	public void setList(List<Role> list) {
		this.list = list;
	}
	
	@RequestMapping("/list")
	public String list(
			@RequestParam("navtabid") String navtabid,
			Model model) {
		list=roleService.getRoleList();
		model.addAttribute("list", list);
		model.addAttribute("navtabid", navtabid);
		return "rolemenu/list";
	}
	
	@RequestMapping("/tree/{id}")
	public String tree(@PathVariable("id") Long id,
			Model model) {
		model.addAttribute("id", id);
		model.addAttribute("userlist",userService.getUserListByRoleid(id));
		return "rolemenu/tree";
	}
	
	
	@RequestMapping("/userrole/{userid}")
	public String userrole(@PathVariable("userid") Long userid,
			Model model) {
		List<Role> list=roleService.getRoleList();
		List<Role> list1=roleService.getRoleListByuserid(userid);
		List<Role> result=Lists.newArrayList();
		for(Role r:list){
			for(Role r1:list1){
				if(r.getId().equals(r1.getId())){
					r.setChecked(true);			
					continue;
				}			
			}
			result.add(r);
		}
		model.addAttribute("list",result);
		model.addAttribute("userid", userid);
		return "user/userrole";
	}
	
	
	@ResponseBody
	@RequestMapping("/saveuserrole/{userid}")
	public AjaxDone saveuserrole(@PathVariable("userid") Long userid,
			@RequestParam("navtabid") String navtabid,HttpServletRequest request
			) 
	{
	//	System.out.println("用户id"+userid);
		
		String[] roleids = request.getParameterValues("roleids"); 
		for(String id:roleids){
			System.out.println(id);
		}
		roleService.saveUserRole(userid,roleids);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setCallbackType("closeCurrent");
		a.setMessage("更新成功");
		a.setForwardUrl("");
	//	a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}
	
	@ResponseBody
	@RequestMapping("/dotree/{id}")
	public List<Menu> dotree(@PathVariable("id") Long id) {
		List<Role> list=roleService.getRoleMenuListByRoleId(id);
		List<Menu> result=menuService.getAllMenuList();
		if(list.size()>0){
			for(Menu m:list.get(0).getMenus())
			{
				for(Menu resultm:result){
					if(resultm.getId().equals(m.getId())){
						resultm.setChecked(true); 
						continue;
					}
				}
			}
		}
		return result;
	}
	
	
	
	@ResponseBody
	@RequestMapping("/savetree/{roleid}")
	public JsonResult savetree(@RequestBody String u,@PathVariable("roleid") Long roleid) {
		ObjectMapper mapper = new ObjectMapper(); 
		try {
			 List<Menu> nodes = mapper.readValue(u, TypeFactory.collectionType(List.class, Menu.class));
			 roleService.insertRolemenu(roleid,nodes);
			 for(Menu m:nodes){
				 System.out.println(m);
			 }
		} catch (JsonParseException e) {
			e.printStackTrace();
			return new JsonResult(false,Exceptions.getStackTraceAsString(e));
		} catch (JsonMappingException e) {
			e.printStackTrace();
			return new JsonResult(false,Exceptions.getStackTraceAsString(e));
		} catch (IOException e) {
			e.printStackTrace();
			return new JsonResult(false,Exceptions.getStackTraceAsString(e));
		}
	
		return new JsonResult(true,"return ok");
	}

}
